package com.mangrove;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

interface OnDownloadListener {
	public void onDownloadComplete(String saveFile);
	public void onDownloadError(int errorCode);
}

class DownloadTask extends AsyncTask<String,Integer,String> {
	
	public static final int DefaultBufferSize = 2 * 1024;
	
	private OnDownloadListener mListener;

	@Override
	protected String doInBackground(String... params) {
		
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(params[0]);
	    HttpResponse resp = null;
	    
	    try {
			resp = client.execute(get);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
		if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			
			HttpEntity entity = resp.getEntity();
			if (entity != null) {
				
				InputStream is = null;
				try {
					is = entity.getContent();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				FileOutputStream fos = null;
				try {
					fos = new FileOutputStream(params[1]);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
				
				byte[] b = new byte[DefaultBufferSize];
				
				try {
					int readCnt = is.read(b);
					while (readCnt != -1) {
						fos.write(b);
						readCnt = is.read(b);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
                
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mListener.onDownloadComplete(params[1]);
			}
		}else {
			mListener.onDownloadError(resp.getStatusLine().getStatusCode());
		}
		return params[1];
	}
	
	@Override
	protected void onPostExecute(String res) {
		
	}
	
	protected void onProgressUpdate(Integer status) {
		
	}
	
	public void setListener(OnDownloadListener listener) {
		mListener = listener;
	}
}

public class DownloadManager implements OnDownloadListener{
	
	private DownloadTask mTask;
	private static DownloadManager mInstance = null;
	
	//test code
	private Context mContext;
	
	private DownloadManager() {
		mTask = new DownloadTask();
	}
	
	public static DownloadManager singleton() {
		if (mInstance == null) {
			mInstance = new DownloadManager();
		}
		
		return mInstance;
	}
	
	public void download(String url,String saveFile) {
		mTask.execute(url,saveFile);
	}

	public void setContext(Context ctx) {
		mContext = ctx;
	}
	
	public void onDownloadComplete(String saveFile) {
		Toast.makeText(mContext, saveFile + "Download Complete", Toast.LENGTH_LONG).show();
	}

	public void onDownloadError(int errorCode) {
		
	}
}
